import pyglet
from pyglet import resource
from pyglet.gl import *
from pyglet.window import key

from pyglons import g
from pyglons.states import _BaseState 

class PyglonsMenuState(_BaseState):

    def __init__(self, options):
        self._basestate.__init__(self)

        opts = dict(halign='center', valign='center')
        self.options = []
        for text, func in options:
            self.options.append(( pyglet.text.Label(text, **opts), func))

        self.option_idx = 0
        self.on_resize(*self.window.get_size())

    def on_enter(self):
        self._basestate.on_enter(self)    
        self.on_draw()

    def on_resize(self, width, height):
        y = height - 100
        x = width / 2
        for label, func in self.options:
            label.x = x
            label.y = y
            y -= 20 + 5

    def on_draw(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()

        for idx, (label, func) in enumerate(self.options):
            label.bold = (idx == self.option_idx)
            label.draw()

    def on_key_press(self, symbol, modifiers):
        if self._basestate.on_key_press(self, symbol, modifiers):
            return
        elif symbol == key.DOWN:
            self.option_idx += 1
            if self.option_idx >= len(self.options):
                self.option_idx = 0
        elif symbol == key.UP:
            self.option_idx -= 1
            if self.option_idx < 0:
                self.option_idx = len(self.options) - 1
        elif symbol in (key.ENTER, key.SPACE):
            self.options[self.option_idx][1]()


